package com.mjfeed.controller.shop;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.ForumReply;
import com.mjfeed.entity.ForumTopic;
import com.mjfeed.entity.Member;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.ForumReplyService;
import com.mjfeed.service.ForumTopicService;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.ProductCategoryService;
/**
 * @description 论坛回复
 * @author zhaominglei
 * @date 2015年1月10日
 */
@Controller("shopForumReplyController")
@RequestMapping("/member/forumReply")
public class ForumReplyController extends BaseController {
    @Resource(name="forumReplyService")
    private ForumReplyService forumReplyService;
    @Resource(name="memberService")
	private MemberService memberService;
    @Resource(name="productCategoryService")
	private ProductCategoryService productCategoryService;
    @Resource(name="forumTopicService")
    private ForumTopicService forumTopicService;
    
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(ForumReply forumReply, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null || forumReply == null 
				|| forumReply.getForumTopicId() == null) {
			return ERROR_VIEW;
		}
		String productCategoryId = request.getParameter("productCategoryId");
		if (productCategoryId == null || productCategoryId.equals("")) {
			return ERROR_VIEW;
		}
		ProductCategory productCategory = productCategoryService.getById(ProductCategory.class, Long.valueOf(productCategoryId));
		if (productCategory == null) {
			return ERROR_VIEW;
		}
		ForumTopic forumTopic = forumTopicService.getById(ForumTopic.class, forumReply.getForumTopicId());
		if (productCategory == null || forumTopic == null) {
			return ERROR_VIEW;
		}
		int replyCount = forumTopic.getReplyCount() + 1;
		forumTopic.setReplyCount(replyCount);
		forumTopicService.update(forumTopic);
		forumReply.setProductCategory(productCategory);
		forumReply.setIp(super.getRemoteIpAddr(request));
		forumReply.setMember(member);
		forumReplyService.save(forumReply);
		return "redirect:/forumTopic/view.shtml?id="+forumReply.getForumTopicId();
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id.longValue() < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("forumReply", forumReplyService.getById(ForumReply.class, id));
		request.setAttribute("productCategoryTree", productCategoryService.findRoots());
		return "shop/forumReply/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(ForumReply forumReply, HttpServletRequest request, HttpServletResponse response, Model model){
		if (forumReply == null || forumReply.getId() == null || forumReply.getForumTopicId() == null) {
			return ERROR_VIEW;
		}
		String productCategoryId = request.getParameter("productCategoryId");
		if (productCategoryId == null || productCategoryId.equals("")) {
			return ERROR_VIEW;
		}
		ProductCategory productCategory = productCategoryService.getById(ProductCategory.class, Long.valueOf(productCategoryId));
		if (productCategory == null) {
			return ERROR_VIEW;
		}
		ForumTopic forumTopic = forumTopicService.getById(ForumTopic.class, forumReply.getForumTopicId());
		if (productCategory == null || forumTopic == null) {
			return ERROR_VIEW;
		}
		ForumReply pForumReply = forumReplyService.getById(ForumReply.class, forumReply.getId());
		if (pForumReply == null) {
			return ERROR_VIEW;
		}
		pForumReply.setProductCategory(productCategory);
		pForumReply.setContent(forumReply.getContent());
		pForumReply.setIp(super.getRemoteIpAddr(request));
		forumReplyService.update(pForumReply);
		return "redirect:/forumTopic/view.shtml?id="+forumReply.getForumTopicId();
	}
	
	@RequestMapping(value = "/delete",method=RequestMethod.GET)
	public String delete(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null) {
			return ERROR_VIEW;
		}
		if (id == null || id.longValue() < 1) {
			return ERROR_VIEW;
		}
		ForumReply forumReply = forumReplyService.getById(ForumReply.class, id);
		if (forumReply == null) {
			return ERROR_VIEW;
		}
		ForumTopic forumTopic = forumTopicService.getById(ForumTopic.class, forumReply.getForumTopicId());
		if (forumTopic == null) {
			return ERROR_VIEW;
		}
		int replyCount = (forumTopic.getReplyCount() - 1) > 0 ? (forumTopic.getReplyCount() - 1) : 0 ;
		forumTopic.setReplyCount(replyCount);
		forumTopicService.update(forumTopic);
		Long forumTopicId= forumReply.getForumTopicId();
		forumReplyService.delete(forumReply);
		return "redirect:/forumTopic/view.shtml?id="+forumTopicId;
	}
}